package com.jjxy.controller;

import com.jjxy.ICheckItemService;
import com.jjxy.entity.PageResult;
import com.jjxy.entity.QueryPageBean;
import com.jjxy.entity.Result;
import com.jjxy.pojo.CheckItem;
import com.jjxy.constant.MessageConstant;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*") // 允许所有域名
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {
    @Resource
    private ICheckItemService checkItemService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.findPage(queryPageBean);
    }

    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody CheckItem checkItem) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 修改为正确的方法名
            checkItemService.add(checkItem);
            result.put("flag", true);
            result.put("message", MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("flag", false);
            result.put("message", MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return result;
    }

    @PostMapping("/edit")
    public Map<String, Object> edit(@RequestBody CheckItem checkItem) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 调用Service层的编辑方法
            // 这里假设Service层有edit方法，需要自行实现
            checkItemService.edit(checkItem);
            result.put("flag", true);
            result.put("message", MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("flag", false);
            result.put("message", MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return result;
    }

    @PostMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Integer> param) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer id = param.get("id");
            // 调用Service层的删除方法
            // 这里假设Service层有delete方法，需要自行实现
            checkItemService.delete(id);
            result.put("flag", true);
            result.put("message", MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("flag", false);
            result.put("message", MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return result;
    }

//    @GetMapping("/findAll")
//    public Result findAll() {
//
//    }
}